package com.atguigu.dga.governance.service.impl;

import com.atguigu.dga.governance.bean.GovernanceAssessTable;
import com.atguigu.dga.governance.bean.GovernanceType;
import com.atguigu.dga.governance.mapper.GovernanceAssessTableMapper;
import com.atguigu.dga.governance.service.GovernanceAssessTableService;
import com.atguigu.dga.governance.service.GovernanceTypeService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 表治理考评情况 服务实现类
 * </p>
 *
 * @author zhangjunyi
 * @since 2023-06-10
 */
@Service
@DS("dga")
public class GovernanceAssessTableServiceImpl extends ServiceImpl<GovernanceAssessTableMapper, GovernanceAssessTable> implements GovernanceAssessTableService {

    @Autowired
    GovernanceAssessTableMapper governanceAssessTableMapper;

    @Autowired
    GovernanceTypeService governanceTypeService;

    @Override
    public void calcGovernanceAssessTable(String assessDate) {
        remove(new QueryWrapper<GovernanceAssessTable>().eq("assess_date", assessDate));

        List<GovernanceAssessTable> governanceAssessTableList = governanceAssessTableMapper.calcScoreByDetail(assessDate);

        //获取到每个维度的权重
        List<GovernanceType> governanceTypeList = governanceTypeService.list();
        //将查询出来的权重表的数据组成KV k：type_code  v:type_weight  组成这个目的是为了方便查询对应维度的权重
        HashMap<String, BigDecimal> typeHashMap = new HashMap<>();
        for (GovernanceType governanceType : governanceTypeList) {
            typeHashMap.put(governanceType.getTypeCode(), governanceType.getTypeWeight());
        }

        //遍历表的集合然后重新对权重分数赋值
        for (GovernanceAssessTable governanceAssessTable : governanceAssessTableList) {
            //获取每个维度的分数*权重
            BigDecimal security = (governanceAssessTable.getScoreSecurityAvg()==null?BigDecimal.valueOf(100):governanceAssessTable.getScoreSecurityAvg()).multiply(typeHashMap.get("SECURITY")).setScale(2, RoundingMode.HALF_UP);
            BigDecimal calc = (governanceAssessTable.getScoreCalcAvg()==null?BigDecimal.valueOf(100):governanceAssessTable.getScoreCalcAvg()).multiply(typeHashMap.get("CALC")).setScale(2, RoundingMode.HALF_UP);
            BigDecimal spec = (governanceAssessTable.getScoreSpecAvg()==null?BigDecimal.valueOf(100):governanceAssessTable.getScoreSpecAvg()).multiply(typeHashMap.get("SPEC")).setScale(2, RoundingMode.HALF_UP);
            BigDecimal storage = (governanceAssessTable.getScoreStorageAvg()==null?BigDecimal.valueOf(100):governanceAssessTable.getScoreStorageAvg()).multiply(typeHashMap.get("STORAGE")).setScale(2, RoundingMode.HALF_UP);
            BigDecimal quality = (governanceAssessTable.getScoreQualityAvg()==null?BigDecimal.valueOf(100):governanceAssessTable.getScoreQualityAvg()).multiply(typeHashMap.get("QUALITY")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal typeScore = security.add(calc).add(spec).add(storage).add(quality);
            governanceAssessTable.setScoreOnTypeWeight(typeScore.movePointLeft(2));

            governanceAssessTable.setCreateTime(new Date());
        }

        //保存到Mysql
        saveBatch(governanceAssessTableList);
    }
}
